/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Interface.Participante;

import static Interface.Global.aux;

import java.awt.Component;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Modelo.Participante.Participante;
import Modelo.Pessoa.Pessoa;
import Util.ExcecaoInstanciaNaoEncontrada;
import java.util.ArrayList;

/**
 *
 * @author ana.siena
 */
public class ConsultarParticipante extends javax.swing.JFrame {

    private long nroInscricao;
    private String nomeParticipante;
    private Participante part;
    private Pessoa pessoa;
    
    
    public ConsultarParticipante() {
        initComponents();
        init();
    }
    
    private void init(){
	setVisible(true);
        setTitle("Consultar Participante");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        nomePart = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        nroPartCombo = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        telefoneParticipante = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        emailParticipante = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        enderecoParticipante = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        dataNascParticipante = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        nomePartEntrada = new javax.swing.JTextField();
        cpfParticipante = new javax.swing.JTextField();
        botaoRetorno = new javax.swing.JButton();
        botaoBuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Colsultar um Participante");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do participante"));

        jLabel2.setText("Nome do participante:");

        nomePart.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomePartActionPerformed(evt);
            }
        });

        jLabel9.setText("Nro do participante:");

        nroPartCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nroPartComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nomePart, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(nroPartCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nomePart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(nroPartCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Seus dados, por favor! ;)"));

        telefoneParticipante.setEnabled(false);
        telefoneParticipante.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                telefoneParticipanteActionPerformed(evt);
            }
        });

        jLabel6.setText("Telefone:");

        jLabel5.setText("E-mail:");

        emailParticipante.setToolTipText("");
        emailParticipante.setEnabled(false);
        emailParticipante.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailParticipanteActionPerformed(evt);
            }
        });

        jLabel3.setText("Nome:");

        jLabel4.setText("CPF:");

        enderecoParticipante.setEnabled(false);
        enderecoParticipante.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                enderecoParticipanteActionPerformed(evt);
            }
        });

        jLabel7.setText("Endereço:");

        dataNascParticipante.setEnabled(false);
        dataNascParticipante.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                dataNascParticipanteActionPerformed(evt);
            }
        });

        jLabel8.setText("Nascimento:");

        nomePartEntrada.setEnabled(false);
        nomePartEntrada.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomePartEntradaActionPerformed(evt);
            }
        });

        cpfParticipante.setEnabled(false);
        cpfParticipante.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfParticipanteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(37, 37, 37)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(emailParticipante)
                    .addComponent(telefoneParticipante)
                    .addComponent(dataNascParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cpfParticipante)
                    .addComponent(nomePartEntrada)
                    .addComponent(enderecoParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nomePartEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cpfParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(enderecoParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(emailParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dataNascParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(telefoneParticipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        botaoRetorno.setText("Retornar");
        botaoRetorno.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoRetornoActionPerformed(evt);
            }
        });

        botaoBuscar.setText("Buscar");
        botaoBuscar.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(109, 109, 109)
                .addComponent(jLabel1)
                .addContainerGap(122, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botaoRetorno)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botaoBuscar))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoRetorno)
                    .addComponent(botaoBuscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dataNascParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataNascParticipanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dataNascParticipanteActionPerformed

    private void nomePartEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomePartEntradaActionPerformed
        
    }//GEN-LAST:event_nomePartEntradaActionPerformed

    private void cpfParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfParticipanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cpfParticipanteActionPerformed

    private void botaoRetornoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoRetornoActionPerformed
        new MenuParticipante().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_botaoRetornoActionPerformed

    private void botaoBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoBuscarActionPerformed
               
        nroInscricao = (long) nroPartCombo.getSelectedItem();
                
        try {
            part = aux.buscaParticipante(nroInscricao);
        } catch (ExcecaoInstanciaNaoEncontrada ex) {
            Logger.getLogger(ConsultarParticipante.class.getName()).log(Level.SEVERE, null, ex);
            Component frame = null;
            JOptionPane.showMessageDialog(frame,"Erro!");
        }
        try {
            //Manda imprimir os dados na tela
            pessoa = aux.buscaPessoa(part.getCodPessoa());
        } catch (ExcecaoInstanciaNaoEncontrada ex) {
            Logger.getLogger(ConsultarParticipante.class.getName()).log(Level.SEVERE, null, ex);
            Component frame = null;
            JOptionPane.showMessageDialog(frame,"Erro!");
        }
        
        nomePartEntrada.setText(pessoa.getNomePessoa());
        cpfParticipante.setText(pessoa.getCpfPessoa());
        enderecoParticipante.setText(pessoa.getEnderecoPessoa());
        emailParticipante.setText(pessoa.getEmailPessoa());
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");       
        String data = df.format(new Date(pessoa.getDataNascPessoa().getTimeInMillis()));
        dataNascParticipante.setText(data);
        telefoneParticipante.setText(pessoa.getTelefonePessoa());
    }//GEN-LAST:event_botaoBuscarActionPerformed

    private void nomePartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomePartActionPerformed
        nomeParticipante = nomePart.getText();
        
        ArrayList <Long> vetorNroInscricao = new ArrayList <Long>();
        vetorNroInscricao = (ArrayList<Long>) aux.buscarParticipantePorNome(nomeParticipante);
        nroPartCombo.removeAllItems();
        for(int i=0; i<vetorNroInscricao.size();i++)
        {
            nroPartCombo.addItem(vetorNroInscricao.get(i));
        }
    }//GEN-LAST:event_nomePartActionPerformed

    private void enderecoParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enderecoParticipanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_enderecoParticipanteActionPerformed

    private void emailParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailParticipanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_emailParticipanteActionPerformed

    private void telefoneParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telefoneParticipanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_telefoneParticipanteActionPerformed

    private void nroPartComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nroPartComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nroPartComboActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoBuscar;
    private javax.swing.JButton botaoRetorno;
    private javax.swing.JTextField cpfParticipante;
    private javax.swing.JTextField dataNascParticipante;
    private javax.swing.JTextField emailParticipante;
    private javax.swing.JTextField enderecoParticipante;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField nomePart;
    private javax.swing.JTextField nomePartEntrada;
    private javax.swing.JComboBox nroPartCombo;
    private javax.swing.JTextField telefoneParticipante;
    // End of variables declaration//GEN-END:variables
}
